from enum import Enum

from flask_sqlalchemy import SQLAlchemy
from conf import mysql_host, mysql_user, mysql_password, mysql_db, mysql_port

db = SQLAlchemy()
# mysql config(s)
dbServer = mysql_host
dbCharset = 'utf8'
dbPort = mysql_port
dbName = mysql_db
dbUser = mysql_user
dbPassword = mysql_password


class UserRole(Enum):
    ADMIN = 'ADMIN'
    USER = 'USER'


class Gender(Enum):
    FEMALE = 'F'
    MALE = 'M'


class User(db.Model):
    query: db.Query = db.session.query_property()
    __tablename__ = 't_user'
    __table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}  # 设置引擎、字符集
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(10), unique=True)
    password = db.Column(db.Text)
    token = db.Column(db.Text)
    role = db.Column(db.Enum(UserRole))


class Advice(db.Model):
    query: db.Query = db.session.query_property()
    __tablename__ = 't_advice'
    __table_args__ = {'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8'}  # 设置引擎、字符集
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user_id = db.Column(db.Integer, db.ForeignKey('t_user.id'), unique=True)
    sub_chat_id = db.Column(db.Integer)
    advice = db.Column(db.Text)


class Conversation(db.Model):
    __tablename__ = 't_conversation'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    chat_id = db.Column(db.Integer)
    start_time = db.Column(db.Time)
    end_time = db.Column(db.Time)
    talker_name = db.Column(db.String(30))
    teacher_name = db.Column(db.String(30))
    pause_time = db.Column(db.Integer)
    content = db.Column(db.String(255))


# connect mysql
def connect():
    return f'mysql+pymysql://{dbUser}:{dbPassword}@{dbServer}:{dbPort}/{dbName}?charset={dbCharset}'
